package com.EAIPClub.CodeShellManager.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.EAIPClub.CodeShellManager.mapper.CodeShellContentManagerMapper;
import com.EAIPClub.CodeShellManager.service.CodeShellContentManagerService;
import com.api.POJO.CodeLab.CodeShellManager.CodeShellManagerContentInfo;
import com.api.POJO.CodeLab.CodeShellManager.CodeShellManagerContentReturnMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description :
 * @Author :
 * @Date : 2021/12/11
 */
@Service
@Slf4j
public class CodeShellContentManagerServiceImpl implements CodeShellContentManagerService {


    private CodeShellContentManagerMapper mapper;

    @Autowired
    public void setMapper(CodeShellContentManagerMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public CodeShellManagerContentReturnMsg addNewCodeShellContent(String content) {
        CodeShellManagerContentReturnMsg returnMsg = new CodeShellManagerContentReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将开始添加实验内容.");
        String uid = getUid();
        log.info("分配uid:{}",uid);
        log.info("即将将信息加入数据库.");
        Boolean aBoolean = mapper.addNewCodeShellContent(new CodeShellManagerContentInfo(null, uid, content));
        if (!aBoolean){
            log.warn("将信息加入数据库失败!uid:{}",uid);
            returnMsg.setStatus(500);
            Msg.add("添加实验信息失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        log.info("添加实验内容成功!uid:{}",uid);
        returnMsg.setStatus(200);
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public CodeShellManagerContentReturnMsg getCodeShellContentByUid(String uid) {
        CodeShellManagerContentReturnMsg returnMsg = new CodeShellManagerContentReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将通过uid查找实验内容.uid:{}",uid);
        if(uid==null || uid.equals("")){
            log.warn("检测到uid为空!");
            returnMsg.setStatus(500);
            Msg.add("uid为空!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        CodeShellManagerContentInfo codeShellContentByUid = mapper.getCodeShellContentByUid(uid);
        if (codeShellContentByUid==null){
            log.warn("未能通过uid查找到实验内容!uid:{}",uid);
            returnMsg.setStatus(500);
            Msg.add("未能查找到实验内容!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        log.info("通过uid查到实验信息.uid:{}",uid);
        returnMsg.setStatus(200);
        returnMsg.setData(codeShellContentByUid);
        return returnMsg;
    }

    @Override
    public CodeShellManagerContentReturnMsg deleteCodeShellContentByUid(String uid) {
        CodeShellManagerContentReturnMsg returnMsg = new CodeShellManagerContentReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将通过uid删除实验内容.uid:{}",uid);
        if (uid ==null || uid.equals("")){
            log.warn("检测到uid为空!");
            returnMsg.setStatus(500);
            Msg.add("uid为空!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        Boolean aBoolean = mapper.deleteCodeShellContentByUid(uid);
        if(!aBoolean){
            log.warn("未能通过uid删除实验内容!uid:{}",uid);
            returnMsg.setStatus(500);
            Msg.add("未能删除实验内容!");
            return returnMsg;
        }
        log.info("通过uid删除实验内容.uid:{}",uid);
        returnMsg.setStatus(200);
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    private String getUid(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return SecureUtil.md5(format.format(new Date()));
    }
}
